module type_casting;

typedef enum { red, blue, green, yellow, white, black } colour;
colour c;
int i;

initial
begin
   i = $cast(c, -5-8); // cast fn. returns an integer. 
                       // if the expression is -5-8, it results in a -ve value
                       // -ve value is out of range
                       // the return value, i, would be 0
                       // the value of c is initialized to the default value
   $display( "i = $cast(c, -5-8);");
   $display("%s, %d", c, i);

   i = $cast(c, -5+8); // if the expr evaluates to a value within the range,
                       // the return value, i, would be 1
   $display( "i = $cast(c, -5+8);");
   $display("%s, %d", c, i);
   
   $cast(c, -5-7);     // cast task
                       // if the expr. evaluates to out of range value,
                       // it results in a RUN TIME ERROR!
   $display( "$cast(c, -5-7);");
   $display("%s", c);  // value of c remains UNCHANGED

   $cast(c, -5+7);     // cast task
   $display( "$cast(c, -5+7);");
   $display("%s", c);   
   
end

endmodule